<?php

namespace App\library\services;

use App\Models\Live;
use App\Models\LiveRecording;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\library\services\lib\tencentCloud\CosLive;
use Illuminate\Support\Facades\Redis;

class LiveService extends BaseServices
{
    protected static $limit = 4;

    /**
     * Author:YangXinPeng
     * 直播推断流回调处理
     * @param $params
     */
    public static function notifyUrl($params)
    {
        $streamId = $params['channel_id'];  //房间号
        $type = $params['event_type'];      //事件类型  1 直播开始  2 直播结束
        $live = Live::where('live_no',$streamId)->get();
        $live && $live = $live->toArray();
        $liveId = $live[0]['id'];
        if ($live) {
            //获取直播记录
            $start_time = strtotime(date('Y-m-d 00:00:00',$params['event_time']));
            $end_time = strtotime(date('Y-m-d 23:59:59',$params['event_time']));
            $liveRecording = LiveRecording::where('live_id',$liveId)->whereBetween('start_time',[$start_time,$end_time])->first();
            $liveRecording && $liveRecording = $liveRecording->toArray();
            //直播记录不存在 记录日志
            if (empty($liveRecording)) {
                if ($type == 0) {  //如果是直播结束回调     那么结束时间那天没找到对应记录，可能是跨天直播 查询前一天记录
                    $start_time = strtotime('-1 day',strtotime($start_time));
                    $end_time = strtotime('-1 day',strtotime($end_time));
                    $liveRecording = LiveRecording::where('live_id',$liveId)->whereBetween('start_time',[$start_time,$end_time])->first();
                    $liveRecording && $liveRecording = $liveRecording->toArray();
                    if (empty($liveRecording)) {
                        $log = ['msg'=>"直播记录未找到",'data'=>$params];
                        file_put_contents('liveRecordNotify.log',json_encode($log),FILE_APPEND);
                        return false;
                    }
                }else {
                    $log = ['msg'=>"直播记录未找到",'data'=>$params];
                    file_put_contents('liveRecordNotify.log',json_encode($log),FILE_APPEND);
                    return false;
                }
            }

            DB::beginTransaction();
            try {
                if ($type == 1) {
                    $startTime = $params['event_time'];
                    //修改直播开始时间
                    Live::where('id',$liveId)->update(['status' => 1, 'start_time' => date('Y-m-d h:i:s',$startTime)]);
                    LiveRecording::where('id',$liveRecording['id'])->update(['status' => 1, 'start_time' => $startTime]);
                } elseif ($type == 0) {
                    //修改直播结束时间
                    $endTime = $params['event_time'];
                    Live::where('id',$liveId)->update(['status' => 2, 'end_time' => date('Y-m-d h:i:s',$endTime)]);
                    LiveRecording::where('id',$liveRecording['id'])->update(['status' => 2, 'end_time' => $endTime]);
                }
                DB::commit();
                return true;
            } catch (\Exception $e) {
                DB::rollBack();
                $log = ['msg'=>$e->getMessage(),'data'=>$params];
                file_put_contents('liveNotify.log',json_encode($log),FILE_APPEND);
                return false;
            }
        } else {
            Log::info('该直播不存在');
            return false;
        }
    }

    /**
     * Author:Zhongyuan Lu
     * 获取推流播流地址
     * @param $streamName
     * @param $year
     * @return string
     */
    public static function getUrl($streamName,$year,$type)
    {
        $instnace = new CosLive();
        $url = $instnace->getUrl($streamName,$year,$type);
        return $url;
    }

    /**
     * Author:Zhongyuan Lu
     * 获取直播间详情
     * @param $id
     * @return array
     */
    public static function getInfo($id)
    {
        $data = Live::find($id);
        $data && $data = $data->toArray();
        return self::success('success',$data);
    }

    /**
     * Author:Zhongyuan Lu
     * 同步数据库
     * @param $live array 推流播流
     * @param $id
     */
    public static function saveUrl($live,$id)
    {
        Live::where('id',$id)->update($live);
    }



    /**
     * Author:Zhongyuan Lu
     * 查询房间号
     * @param $live_no
     * @return mixed
     */
    public static function hasLiveNo($live_no)
    {
        return Live::where('live_no',$live_no)->value('id');
    }
}
